package org.exst.open.certsoftware.exemplos;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;

import org.exst.open.certsoftware.CertificationManager;
import org.exst.open.certsoftware.InvalidHashException;

/**
 * @author Nuno Antunes
 *
 */
public class VerificacaoAssinaturas {

	public static void main(String[] args) {

		String sampleData = "2010-05-18;2010-05-18T11:22:19;FAC 001/14;3.12;";
		
		CertificationManager cm = CertificationManager.getInstance();
		try {
			cm.readFromFilePrivateKey("cert/exemplo/DER/exemplo-private-key-pkcs8.der");
			cm.readFromFilePublicKey("cert/exemplo/DER/exemplo-public-key.der");
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}
		
		String hashBase64;
		byte[] hashBinary;
		
		try {

			hashBase64 = cm.getHashBase64(sampleData);
			hashBinary = cm.getHashBinary(sampleData);

			System.out.println("Computed Hash (Base64): ");
			System.out.println(hashBase64);
			System.out.println("Tamanho da Hash: " + hashBase64.length() + " bytes");
			System.out.println("Verification Base64: " + (cm.verifyHashBase64(sampleData, hashBase64) ? "OK" : "failure"));				
			System.out.println("Verification Binary: " + (cm.verifyHashBinary(sampleData, hashBinary) ? "OK" : "failure"));				
			
		} catch (InvalidHashException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}
		
		
	}
	
}
